package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.BspsMchntBaseInfoMapper;
import cc.rengu.igas.bsps.common.dao.BspsNoTxnRegMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsMchntBaseInfoMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.BspsNoTxnRegMapperImpl;
import cc.rengu.igas.bsps.common.entity.MchntBaseInfo;
import cc.rengu.igas.bsps.common.entity.NoTxnReg;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.common.enums.RoleTypeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.bean.NoTransMchntInfoBean;
import cc.rengu.igas.bsps.facade.request.NoTransMchntListQueryRequest;
import cc.rengu.igas.bsps.facade.response.NoTransMchntListQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.StringUtil;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * 无交易商户列表查询
 * Created by 王成 on 2020/3/25
 */
public class NoTransMchntListQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        NoTransMchntListQueryRequest noTransMchntListQueryRequest = new NoTransMchntListQueryRequest();
        ConvertUtil.convertOutput(noTransMchntListQueryRequest);
        return noTransMchntListQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        NoTransMchntListQueryRequest noTransMchntListQueryRequest = (NoTransMchntListQueryRequest) request;
        NoTransMchntListQueryResponse noTransMchntListQueryResponse = new NoTransMchntListQueryResponse();

        String instId = noTransMchntListQueryRequest.getHeader().getInstId();
        String queryType = noTransMchntListQueryRequest.getQueryType();
        String userId = noTransMchntListQueryRequest.getUserId();
        String startDate = noTransMchntListQueryRequest.getStartDate();
        String endDate = noTransMchntListQueryRequest.getEndDate();
        String queryId = noTransMchntListQueryRequest.getQueryId();
        /* 参数合法性校验 */
        rglog.info("必填性校验");
        if (StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(queryType) || StringUtil.isEmptyOrNull(queryId) ||
                StringUtil.isEmptyOrNull(startDate) || StringUtil.isEmptyOrNull(endDate)) {
            rglog.error("用户编号<{}>,查询类型<{}>,查询主体<{}>,开始日期<{}>,截止日期<{}>不能为空!", userId, queryType, queryId, startDate, endDate);
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        rglog.info("校验查询日期范围");
        if (DateUtil.compareDate(startDate, endDate) > 0 || DateUtil.compareDate(DateUtil.getBaseDateOffsetMonth(startDate, 12), endDate) < 0) {
            rglog.info("查询时间超范围");
            throw new BizException(RespCodeEnum.DATESPAN_CHECK_ERROR.getRespCode(), RespCodeEnum.DATESPAN_CHECK_ERROR.getRespDesc());
        }

        /*查询交易数据*/
        rglog.info("分页信息赋值");
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageIndex(noTransMchntListQueryRequest.getPageIndex());
        pageInfo.setPageSize(noTransMchntListQueryRequest.getPageSize());
        BspsNoTxnRegMapper bspsNoTxnRegMapper = new BspsNoTxnRegMapperImpl();
        PageResult pageResult = null;
        if (queryType.equals(RoleTypeEnum.GROUP_MCHNT.getRoleType())) {
            pageResult = bspsNoTxnRegMapper.selectBspsNoTxnRegByGroupMchntNoPage(instId, queryId, startDate, endDate, pageInfo);
        } else if (queryType.equals(RoleTypeEnum.PLAT_MCHNT.getRoleType())) {
            pageResult = bspsNoTxnRegMapper.selectBspsNoTxnRegByPlatMchntNoPage(instId, queryId, startDate, endDate, pageInfo);
        } else if (queryType.equals(RoleTypeEnum.AGENT_MCHNT.getRoleType())) {
            pageResult = bspsNoTxnRegMapper.selectBspsNoTxnRegByAgentMchntNoPage(instId, queryId, startDate, endDate, pageInfo);
        } else if (queryType.equals(RoleTypeEnum.MANAGER.getRoleType())) {
            pageResult = bspsNoTxnRegMapper.selectBspsNoTxnRegByManagerIdPage(instId, queryId, startDate, endDate, pageInfo);
        } else {
            rglog.error("非法操作，不支持的操作类型:<{}>", queryType);
            throw new BizException(RespCodeEnum.ILLEGAL_OPERATION.getRespCode(), RespCodeEnum.ILLEGAL_OPERATION.getRespDesc());
        }

        List<NoTransMchntInfoBean> noTransMchntInfos = new ArrayList<>();
        if (null != pageResult) {
            for (Object object : pageResult.getResult()) {
                NoTxnReg noTxnReg = (NoTxnReg) object;
                convNoTransMchntInfo(noTxnReg, noTransMchntInfos);
            }
            noTransMchntListQueryResponse.setTotalPage(pageResult.getPageInfo().getTotalPage());
            noTransMchntListQueryResponse.setTotalRows(pageResult.getPageInfo().getTotalRows());
        }
        noTransMchntListQueryResponse.setNoTransMchntInfoList(noTransMchntInfos);
        /* 组装应答报文 */
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(noTransMchntListQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        NoTransMchntListQueryResponse noTransMchntListQueryResponse = (NoTransMchntListQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(noTransMchntListQueryResponse);
    }

    /**
     * 无交易商户终端信息转换
     *
     * @param noTxnReg          无交易商户终端信息
     * @param noTransMchntInfos 接口返回商户终端信息
     */
    private void convNoTransMchntInfo(NoTxnReg noTxnReg, List<NoTransMchntInfoBean> noTransMchntInfos) {
        NoTransMchntInfoBean noTransMchntInfo = new NoTransMchntInfoBean();
        noTransMchntInfo.setMchntNo(noTxnReg.getMchntNo());
        noTransMchntInfo.setMchntName(noTxnReg.getMchntName());
        noTransMchntInfo.setMchntType(noTxnReg.getIndustryType());
        noTransMchntInfo.setManagerId(noTxnReg.getManagerId());
        noTransMchntInfo.setManagerPhone(noTxnReg.getManagerPhone());
        noTransMchntInfo.setMchntContactName(noTxnReg.getMchntContactPerson());
        noTransMchntInfo.setMchntContactPhone(noTxnReg.getMchntContactPhone());
        noTransMchntInfo.setMchntStatus(noTxnReg.getMchntStatus());
        noTransMchntInfo.setMchntExpandDate(noTxnReg.getMchntExpandDate());
        noTransMchntInfo.setLastTxnTime(noTxnReg.getEndTxnTime());
        noTransMchntInfo.setRemark(noTxnReg.getRemark1());
        BspsMchntBaseInfoMapper bspsMchntBaseInfoMapper = new BspsMchntBaseInfoMapperImpl();
        try {
            MchntBaseInfo mchntBaseInfo = bspsMchntBaseInfoMapper.selectBspsMchntBaseInfoByPrimaryKey(noTxnReg.getInstId(), noTxnReg.getMchntNo());
            if (null != mchntBaseInfo) {
                noTransMchntInfo.setManagerName(mchntBaseInfo.getManagerName());
                noTransMchntInfo.setMchntType(mchntBaseInfo.getMchntType());
            }
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("获取商户基本信息失败:{}", byteArrayOutputStream.toString());
        }
        noTransMchntInfos.add(noTransMchntInfo);
    }
}
